package com.example.peibei.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dingtao.common.bean.Order;
import com.dingtao.common.bean.RefreshResult;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDFragment;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.HomeOrderPresenter;
import com.example.peibei.ui.activity.OrderDetailActivity;
import com.example.peibei.ui.activity.PersonInfoActivityTwo;
import com.example.peibei.ui.activity.PersonalInfoActivity;
import com.example.peibei.ui.adapter.HomeOrderListAdapter;
import com.example.peibei.ui.weight.EmptyRecyclerView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class OrderListFragment extends WDFragment {

    @BindView(R.id.rv_order_list)
    EmptyRecyclerView orderList;
    @BindView(R.id.empty_view)
    LinearLayout empty_view;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    String type = "";
    List<Order.RecordsDTO> orders;
    private HomeOrderListAdapter homeOrderListAdapter;
    private HomeOrderPresenter homeOrderPresenter;
    private SPUtils spUtils;
    private String cityCode;
    private String cityName;

    public OrderListFragment(String type) {
        this.type = type;
    }

    @Override
    public String getPageName() {
        return "订单列表fragment";
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_order_list;
    }

    @Override
    protected void initView() {
        spUtils = new SPUtils(getActivity(), SpConstant.USER_TOKEN);
        cityCode = spUtils.getString(SpConstant.CITY_CODE);
        cityName = spUtils.getString(SpConstant.CITY_NAME);
        orders = new ArrayList<>();
        orderList.setLayoutManager(new LinearLayoutManager(getContext()));
        homeOrderListAdapter = new HomeOrderListAdapter(orders);
        orderList.setAdapter(homeOrderListAdapter);
        orderList.setEmptyView(empty_view);

        //获取数据
        homeOrderPresenter = new HomeOrderPresenter(new OrderCall());
        onRefreshStart();
        homeOrderListAdapter.addChildClickViewIds(R.id.iv_head);
        homeOrderListAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.iv_head) {
                Order.RecordsDTO order = (Order.RecordsDTO) adapter.getData().get(position);
                Bundle bundle = new Bundle();
                bundle.putString("mid", order.getMid());
                intent(PersonInfoActivityTwo.class, bundle);
            }
        });
        homeOrderListAdapter.setOnItemClickListener((adapter, view, position) -> {
            Order.RecordsDTO order = (Order.RecordsDTO) adapter.getData().get(position);
            String publishOrderId = order.getPublishOrderId();
            Bundle bundle = new Bundle();
            bundle.putString("publishOrderId", publishOrderId);
            intent(OrderDetailActivity.class, bundle);
        });
        refreshLayout.setOnRefreshListener(refreshLayout -> onRefreshStart());
        refreshLayout.setOnLoadMoreListener(refreshLayout -> onLoadMoreStart());
    }

    public void updateData(String cc, String cn) {
        cityCode = cc;
        cityName = cn;
        if (homeOrderPresenter != null) {
            homeOrderPresenter.reqeust(true, "", type, cc, cn);
        }
    }

    public void updateDataTwo() {
        if (homeOrderPresenter != null) {
            homeOrderPresenter.reqeust(true, "", type, "", "");
        }
    }

    public void onRefreshStart() {
        if (homeOrderPresenter.isRunning()) {
            refreshLayout.finishRefresh();
            return;
        }
        if (type.equals("1")) {
            homeOrderPresenter.reqeust(true, "", type, "", "");
        } else {
            homeOrderPresenter.reqeust(true, "", type, cityCode, cityName);
        }
    }

    public void onLoadMoreStart() {
        if (homeOrderPresenter.isRunning()) {
            refreshLayout.finishLoadMore();
            return;
        }
        if (type.equals("1")) {
            homeOrderPresenter.reqeust(false, "", type, "", "");
        } else {
            homeOrderPresenter.reqeust(false, "", type, cityCode, cityName);
        }
    }

    class OrderCall implements DataCall<Order> {

        @Override
        public void success(Order data, Object... args) {
            refreshLayout.finishRefresh();
            refreshLayout.finishLoadMore();
            //添加列表并刷新
            if (homeOrderPresenter.getPage() == 1) {
                orders.clear();
            }
            orders.addAll(data.getRecords());
            homeOrderListAdapter.notifyDataSetChanged();
        }

        @Override
        public void fail(ApiException e, Object... args) {
            refreshLayout.finishRefresh();
            refreshLayout.finishLoadMore();
        }
    }
}
